package com.ehotting.edsta.cae.controller;

import com.ehotting.eaf.core.StringHelper;
import com.ehotting.eaf.core.domain.R;
import com.ehotting.eaf.core.utils.NumberHelper;
import com.ehotting.eaf.security.service.TokenService;

import com.ehotting.mqbd.api.MqActionConstants;
import com.ehotting.mqbd.api.bean.*;
import com.ehotting.mqbd.api.enums.TopicEnum;
import com.ehotting.mqbd.core.service.MQService;
import com.ehotting.mqbd.core.manager.AgentLinkManager;
import com.ehotting.mqbd.core.web.MqWebHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

@RestController
@RequestMapping
@Api(tags = "CaeController",description = "多物理CAE转发controller")
public class CaeController {

    private final static long httpTimeout = 60 * 20;

    @Resource(name="mqService")
    private MQService mqService;
    @Autowired
    private MqApplicationContext mqApplicationContext;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private AgentLinkManager agentLinkManager;

    @ApiOperation("多物理CAE请求包转发")
    @PostMapping("/rePost")
    public R<CommonResponse> rePost(@RequestBody CommonRequest request) {
        Long userId = tokenService.getLoginUser().getUserid();

        String mainProjectId = NumberHelper.isDigital(request.getMainProjectId())?request.getMainProjectId():"0";
        String agentId = agentLinkManager.genAgentId(mqApplicationContext.getModule(),userId,mainProjectId);

        MQHttpData data = MQHttpData.create(
                TopicEnum.CONCURRENCY,
                mqApplicationContext, agentId,
                "");
        data.setAction(MqActionConstants.RE_POST);
        data.setRequest(request);
        data.setAuthorization(tokenService.getJwtByPriKey("", "him2wsx#EDC"));
        data.setLogRequest(true);
        data.setLogResponse(true);
        FutureTask<MQDataAble> futureTask = mqService.requestFuture(data,httpTimeout);
        return MqWebHelper.handleResult(futureTask,data);
    }

    @ApiOperation("多物理CAE上传文件的请求包转发")
    @PostMapping("/rePostUpload")
    public R<CommonResponse> rePostUpload(@RequestBody CommonRequest request) {

        Long userId = tokenService.getLoginUser().getUserid();

        String mainProjectId = NumberHelper.isDigital(request.getMainProjectId())?request.getMainProjectId():"0";
        String agentId = agentLinkManager.genAgentId(mqApplicationContext.getModule(),userId,mainProjectId);

        MQHttpData data = MQHttpData.create(
                TopicEnum.CONCURRENCY,
                mqApplicationContext,agentId,
                "");
        data.setAction(MqActionConstants.RE_UPLOAD);
        data.setRequest(request);
        data.setAuthorization(tokenService.getJwtByPriKey("","him2wsx#EDC"));
        data.setLogRequest(true);
        data.setLogResponse(true);
        FutureTask<MQDataAble> futureTask = mqService.requestFuture(data,httpTimeout);
        return MqWebHelper.handleResult( futureTask,data);
    }
}